require 'rake'
require 'packr'

FILES = %w(yahoo dom event selector animation connection get json cookie history)

task :extract do
  version = ENV['v']
  return unless File.directory?(version)
  copyright = nil
  code = FILES.inject("") do |source, file|
    name = "#{version}/build/#{file}/#{file}"
    name = "#{name}-beta" if File.file?("#{name}-beta.js")
    min = ENV['d'] ? '' : '-min'
    name = File.file?("#{name}-min.js") ? "#{name}#{min}.js" : "#{name}.js" 
    if File.file?(name)
      content = File.read(name)
      source += "\n" + content
      copyright ||= content.scan(/\/\*.*?\*\//m).first
      puts " * #{name}"
    end
    source
  end
  code = copyright + "\n" + (ENV['d'] ? code : Packr.pack(code, :shrink_vars => true))
  File.open("#{version}.js", 'wb') { |f| f.write(code) }
end
